﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Assist;

namespace DBService
{
	public partial class DlgLogin : System.Windows.Forms.Form
	{
		public DlgLogin()
		{
			this.InitializeComponent();
		}

		public DbUser User
		{
			get
			{
				return this.mUser;
			}
		}

		private void button1_Click(object sender, EventArgs e)
		{
			try
			{
				if (this.txtName.Text.Trim() != "")
				{
					DbUser user = DbUser.GetUser(this.txtName.Text.Trim(), BimServerDB.Default);
					if (user == null)
					{
						MessageBox.Show("用户名输入错误。");
						return;
					}
					if (!user.Password.Equals(DbUser.ToMd5(this.txtPass.Text)))
					{
						MessageBox.Show("密码输入错误。");
						return;
					}
					this.mUser = user;
					if (user.Status == UserStatus.Registered)
					{
						if (this.cbxAuto.CheckState == CheckState.Checked)
						{
							string fileName = AssistFunc.GetAppRootPath() + "\\Data\\Config.xml";
							XmlUtils.WriteConfigFile(fileName, "AutoUser", "value", this.txtName.Text.Trim());
							XmlUtils.WriteConfigFile(fileName, "AutoPwd", "value", this.txtPass.Text);
						}
						else
						{
							string fileName2 = AssistFunc.GetAppRootPath() + "\\Data\\Config.xml";
							XmlUtils.WriteConfigFile(fileName2, "AutoUser", "value", "");
							XmlUtils.WriteConfigFile(fileName2, "AutoPwd", "value", "");
						}
					}
				}
				base.DialogResult = DialogResult.OK;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		private void btnConfig_Click(object sender, EventArgs e)
		{
			new DlgConfig().ShowDialog(this);
		}

		private void DlgLogin_Load(object sender, EventArgs e)
		{
			this.ReadData();
		}

		private void DlgLogin_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveData();
		}

		public static bool IsAutoLogin()
		{
			string path = Path.GetTempPath() + "DlgLogin.dat";
			if (!File.Exists(path))
			{
				return true;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			Convert.ToInt32(streamReader.ReadLine());
			Convert.ToInt32(streamReader.ReadLine());
			streamReader.ReadLine();
			bool result = Convert.ToBoolean(streamReader.ReadLine());
			streamReader.Close();
			fileStream.Close();
			return result;
		}

		private void ReadData()
		{
			string path = Path.GetTempPath() + "DlgLogin.dat";
			if (!File.Exists(path))
			{
				base.StartPosition = FormStartPosition.CenterScreen;
				this.txtName.Text = "";
				this.cbxAuto.Checked = true;
				return;
			}
			base.StartPosition = FormStartPosition.Manual;
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			int x = Convert.ToInt32(streamReader.ReadLine());
			int y = Convert.ToInt32(streamReader.ReadLine());
			base.Location = new System.Drawing.Point(x, y);
			this.txtName.Text = streamReader.ReadLine();
			this.cbxAuto.Checked = Convert.ToBoolean(streamReader.ReadLine());
			streamReader.Close();
			fileStream.Close();
		}

		private void SaveData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "DlgLogin.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.WriteLine(this.txtName.Text);
			streamWriter.WriteLine(this.cbxAuto.Checked);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		public DbUser mUser;
	}
}
